<%--JINJIANG OLD Vesion 2.6--%>
<%@ page language="java" import="org.w3c.dom.*,javax.xml.parsers.*,javax.xml.transform.*,javax.xml.transform.stream.StreamResult,javax.xml.transform.Transformer,javax.xml.transform.dom.DOMSource,java.util.*,java.net.*,java.io.*,javax.naming.InitialContext"
	pageEncoding="gb2312"%>
<%!
	private String getElementValue(String keyvalue,String defaultvalue,Element elem){
		String result=defaultvalue;
		if(elem.getElementsByTagName(keyvalue).getLength()!=0){
			if(elem.getElementsByTagName(keyvalue).item(0).getFirstChild()!=null){
				result=elem.getElementsByTagName(keyvalue).item(0).getFirstChild().getNodeValue();
				if(result.equals("")){
					result=defaultvalue;
				} 
			} 
		}
		return result; 
	}
	private String getDocValue(String keyvalue,String defaultvalue,Document elem){
		String result=defaultvalue; 
		if(elem.getElementsByTagName(keyvalue).getLength()!=0){
			if(elem.getElementsByTagName(keyvalue).item(0).getFirstChild()!=null){
				result=elem.getElementsByTagName(keyvalue).item(0).getFirstChild().getNodeValue();
				if(result.equals("")){
					result=defaultvalue;
				}
			}  
		} 
		return result;
	}

	private String[] readServerXml(){
		String xmluri=System.getProperty("jboss.server.home.dir")+"/deploy/jboss-web.deployer/ROOT.war/pms/server.xml";
	  	String mainFile="pms.exe";
	  	String serverxml="";
	  	String versionxml="";
		String modelxml="";
		String hotelxml="";
		DocumentBuilderFactory builder=DocumentBuilderFactory.newInstance();
		try {
				DocumentBuilder db=builder.newDocumentBuilder();
				Document doc=db.parse(xmluri);
				Element versionel=null;
				String rmiport=getDocValue("rmiport","9070",doc);
				serverxml+="rmiport:"+rmiport+"+";
				String systemname=getDocValue("systemname","Pms",doc);
				systemname=systemname.substring(0,1).toUpperCase()+systemname.substring(1).toLowerCase();
				serverxml+="systemname:"+systemname+"+";
				String training=getDocValue("training","",doc);
				serverxml+="training:"+training+"+";
				String cluster=getDocValue("cluster","false",doc);
				serverxml+="cluster:"+cluster+"+";
				String downloadpath=getDocValue("download-path","",doc);
				serverxml+="download-path:"+downloadpath.replace(":","|")+"+";
				String temcolor=getDocValue("color","194, 204, 198",doc);
				serverxml+="color:"+temcolor+"+";
				NodeList list=doc.getElementsByTagName("downfile");
				boolean hasmain=false;
				String homeurl="";
				for(int i=0;i<list.getLength();i++){
					versionel=(Element)list.item(i);
					String type=versionel.getAttribute("type");
					String filename=getElementValue("name",null,versionel);
						if(filename!=null){
							String version=getElementValue("version",null,versionel);
							String furl=getElementValue("url","",versionel);
							if(furl!=null&&!furl.equals("")){
								versionxml+=filename+":"+ version+"["+furl+",";
							}
							else{
								versionxml+=filename+":"+ version+",";
							}
							if(type.trim().equalsIgnoreCase("main")){
								homeurl=furl;
								if(homeurl.equals("")){
									homeurl=downloadpath+"/"+systemname+"/";
									furl=homeurl;
								}
							}
							
					}
			}
			NodeList jmslist=doc.getElementsByTagName("jms");
			if(jmslist!=null){
				versionel=(Element)jmslist.item(0);
				  if(versionel!=null){
				  String value=versionel.getAttribute("value");
				  if(!(value==null||value.equals("false"))){
					String hjndi=getElementValue("port",null,versionel);
					if(!(hjndi==null||hjndi.equals("")))
						serverxml+="JMSPort:"+hjndi+"+";
				  }
				}
			}
			Element modelversionel=(Element)doc.getElementsByTagName("model").item(0);
			NodeList modellist=modelversionel.getElementsByTagName("name");
			for(int i=0;i<modellist.getLength();i++){
				versionel=(Element)modellist.item(i);
				String value=versionel.getAttribute("value");
				String modelname=versionel.getFirstChild().getNodeValue();
				if(!(value==null || value.equals(""))){
					modelxml+=value+"/"+modelname+",";
				}
				else{
					modelxml+=i+"/"+modelname+",";
				}

			}
			Element hotelversionel=(Element)doc.getElementsByTagName("hotel").item(0);
			NodeList hotellist=hotelversionel.getElementsByTagName("hotelid");
			for(int i=0;i<hotellist.getLength();i++){
				versionel=(Element)hotellist.item(i);
				String value=versionel.getAttribute("value");
				String hotelname=versionel.getFirstChild().getNodeValue();
				if(!(value==null || value.equals(""))){
					hotelxml+=value+"/"+hotelname+",";
				}
				else{
					hotelxml+=i+"/"+hotelname+",";
				}

			}

		}catch(Exception e){
			e.printStackTrace();
		}
		return new String[]{serverxml,versionxml,modelxml,hotelxml};
	}
	%>
<%
  String url = request.getScheme()+"://"+request.getServerName()    +":"+request.getServerPort();
  String ip=request.getServerName();
  String[] serverxml=readServerXml();
%>
<script language="JavaScript">
 var exp = new Date();
 exp.setTime(exp.getTime() + 30*24*3600*1000);
 document.cookie ='pmsurl=' + <%="'"+url+"'"%>+ ';expires=' + exp.toGMTString();
 document.cookie ='pmsip=' + <%="'"+ip+"'"%>+ ';expires=' + exp.toGMTString();
 document.cookie ='serverxml1=' + <%="'"+serverxml[0]+"'"%>+ ';expires=' + exp.toGMTString();
 document.cookie ='serverxml2=' + <%="'"+serverxml[1]+"'"%>+ ';expires=' + exp.toGMTString();
 document.cookie ='serverxml3=' + <%="'"+serverxml[3]+"'"%>+ ';expires=' + exp.toGMTString();
 document.cookie ='modelxml=' + <%="'"+serverxml[2]+"'"%>+ ';expires=' + exp.toGMTString();
</script> 
